// request.js
function request(url, data = {}, method = 'GET') {
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: '加载中...'
    });
    wx.request({
      url: 'http://localhost:9001/' + url, 
      data: data,
      method: method,
      withCredentials: true,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        wx.hideLoading();
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error('请求失败，状态码：' + res.statusCode));
        }
      },
      fail: (err) => {
        wx.hideLoading();
        reject(new Error('网络请求失败：' + err.errMsg));
      }
    });
  });
}

export default request;
